

function showDetail() {
    $(".detailWindow").show();
    $(".detailWindow .detailBody .pagerInfo").remove();
    $(".detailWindow .tabPage").remove();
    if ($(".detailWindow").hasClass("TA"))
        $(".detailWindow").css({"width": "84%", "left": "calc( 8% )", "top": "6%", "height": "85%", "opacity": "1"});
    setTimeout(function () {
        resizeDetail();
    }, 500)
}

function resizeDetail() {
    $(".tabPages").height($(".detailBody").height() - $(".tabPage").prevAll().height() - 90);
}

function hideDetail() {
    $(".detailWindow").css({"width": "80%", "left": "calc( 10% )", "top": "10%", "height": "80%", "opacity": "0"});
    setTimeout(function () {
        $(".detailWindow").hide();
    }, 300)
}

function getGroupObj(title) {
    return $(
        '<div class="group">' +
        '   <div class="groupName">' + title + '</div>' +
        '   <div class="groupContent">' +
        '   <table border="0" cellpadding="0" cellspacing="0"></table>' +
        '   </div>' +
        '</div>'
    );
}

function setTabPage(groups) {
    var tabPage = $("<div class='tabPage'></div>")
    var buttons = $("<div class='tabButtons'></div>")
    var pages = $("<div class='tabPages'></div>")
    tabPage.append(buttons).append(pages);
    groups[0].before(tabPage);
    $(groups).each(function (index, item) {
        buttons.append(item.find(".groupName"));
        pages.append(item.find(".group"));
        item.remove();
    });
    bindTab(buttons, "groupName", pages);
}

var detailParam = {
    data: {
        pageSize: 10
    }
};

function loadDetail_mxjb(param, disTab) {
            if (param.MedId == 1) {
                param.ybType = "省医保"
            } else if (param.MedId == 2) {
                param.ybType = "市医保"
            }
            showDetail();
            $(".detailWindow .title").html(param.title);
            $(".detailWindow .subTitle").html(param.Drgsname + "<span>" + param.ybType + "</span>");
            $(".detailWindow .ruleInfo").html("");
            $(".detailWindow .detailFoot .info").html("<h3>启用日期：" + param.dstartdate + "</h3>");
            $(".detailWindow .tableGroups").remove("");

            var tableGroup1 = $("<div class='tableGroups'></div>");
            $(".detailWindow .detailBody").append(tableGroup1);

            //疾病
            var url = "/standard/chronicRule/queryDetailList/" + param.ChronicRuleId + "/Drg";
            var groupDrg = getGroupObj("适用疾病");
            tableGroup1.append(groupDrg);
            loadServerData(url, null, function (data) {
                for (var i = 0; i < data.datas.length; i++) {
                    var tr = $("<tr></tr>");
                    groupDrg.find("table").append(tr);
                    for (var j = 0; j < 3; j++) {
                        if (i < data.datas.length) {
                            tr.append($("<td>" + data.datas[i].Icdname + "【" + data.datas[i].Icdcode + "】</td>"));
                            i++;
                        } else {
                            break;
                        }
                    }
                }
                resizeDetail();
            })


            var tableGroup3 = $("<div class='tableGroups'></div>");
            $(".detailWindow .detailBody").append(tableGroup3);
            //费用明细
            var url = "/standard/chronicRule/queryDetailList/" + param.ChronicRuleId + "/Fare";
            var groupFare = getGroupObj("费用明细");
            tableGroup3.append(groupFare);
            loadServerData(url, null, function (data) {
                for (var i = 0; i < data.datas.length;) {
                    var tr = $("<tr></tr>");
                    groupFare.find("table").append(tr);
                    for (var j = 0; j < 2; j++) {
                        if (i < data.datas.length) {
                            tr.append($("<td>" + data.datas[i].cdrugname + "【" + data.datas[i].nnum + "次/月】</td>"));
                            i++;
                        } else {
                            break;
                        }
                    }
                }

            })

            var tableGroup2 = $("<div class='tableGroups'></div>");
            $(".detailWindow .detailBody").append(tableGroup2);
            //用药明细
            var url = "/standard/chronicRule/queryDetailList/" + param.ChronicRuleId + "/Drug";
            var groupDrug = getGroupObj("用药明细");
            tableGroup2.append(groupDrug);
            var render = function (data) {
            groupDrug.find("table").html("");
            for (var i = 0; i < data.datas.length;) {
                var tr = $("<tr></tr>");
                groupDrug.find("table").append(tr);
                for (var j = 0; j < 1; j++) {
                    if (i < data.datas.length) {
                        tr.append($("<td>" + data.datas[i].cDgGeneralName + "</td><td>" + data.datas[i].cDgSpecification + "-" + data.datas[i].jx + "-" + data.datas[i].cManufacturer + "</td><td>单次" + data.datas[i].everydrugnum + " 用药" + data.datas[i].drugsaynum + "天</td>"));
                        i++;
                    } else {
                        break;
                    }
                }
            }

            var datas = data.datas;
            var countAll = data.totalSize;
            var count = data.datas.length;
            var countPage = Math.ceil(countAll / detailParam.data.pageSize);
            detailParam.data.countPage = countPage;
            detailParam.data.countAll = countAll;
            var nowPage = detailParam.data.pageNum;
            var pageCount = detailParam.data.countPage;
            var countAll = detailParam.data.countAll;
            var startPage = 1;

            var ul = $(".detailWindow .detailBody .pagerInfo");
            if (ul.length == 0) {
                ul = $("<div class='pagerInfo'></div>");
            }
            groupDrug.append(ul);
            ul.html("");
            ul.append($("<span>共" + countAll + "条记录  " + nowPage + "/" + pageCount + "页</span>"));

            ul.append($("<span class='right'><a>末页</a></span>").bind("click", function () {
                gotoPageDetail(null, pageCount, url, render)
            }));
            ul.append($("<span class='right'><a>下一页</a></span>").bind("click", function () {
                gotoPageDetail(null, detailParam.data.pageNum + 1, url, render)
            }));
            var pageBox = $($("<span class='right'></span>"));
            ul.append(pageBox);
            ul.append($("<span class='right'><a>上一页</a></span>").bind("click", function () {

                gotoPageDetail(null, detailParam.data.pageNum - 1, url, render)
            }));
            ul.append($("<span class='right'><a>首页</a></span>").bind("click", function () {
                gotoPageDetail(null, 1, url, render)
            }));
    };
    gotoPageDetail(null, 1, url, render);
    if (!disTab) {
        setTabPage([tableGroup3, tableGroup2]);
    }

}

function gotoPageDetail(e, page, url, render) {
    if (page == null) {
        page = $(e.target).text();
    }
    if (page > detailParam.data.countPage || page == 0) return;
    detailParam.data.pageNum = page;
    loadServerData(url, detailParam.data, render)
}

function loadDetail_dbz(param) {
    showDetail();
    $(".detailWindow .title").html(param.title);
    $(".detailWindow .subTitle").html(param.Drgsname + "<span>" + param.ybType + "</span>");
    $(".detailWindow .ruleInfo").html("");
    $(".detailWindow .ruleInfo").append('<div class="ruleTitle">核定金额：' + param.fixedmoney + '元</div>');
    $(".detailWindow .detailFoot .info").html("<h3>启用日期：" + (param.dstartdate || "") + "</h3>");
    $(".detailWindow .tableGroups").remove("");

    var tableGroup1 = $("<div class='tableGroups'></div>");
    $(".detailWindow .detailBody").append(tableGroup1);
    //疾病
    var url = "/standard/drgRule/queryDetailList/" + param.iDrgsId + "/Drg";
    var groupDrg = getGroupObj("适用疾病");
    tableGroup1.append(groupDrg);
    loadServerData(url, null, function (data) {
        for (var i = 0; i < data.datas.length;) {
            var tr = $("<tr></tr>");
            groupDrg.find("table").append(tr);
            for (var j = 0; j < 1; j++) {
                if (i < data.datas.length) {
                    tr.append($("<td>" + data.datas[i].Icdname + "【" + data.datas[i].Icdcode + "】</td>"));
                    tr.append($("<td>" + (data.datas[i].treatwayname||"无治疗方式说明") + "</td>"));
                    i++;
                } else {
                    break;
                }
            }
        }

    })

    var tableGroup2 = $("<div class='tableGroups'></div>");
    $(".detailWindow .detailBody").append(tableGroup2);
    //费用类型明细
    var url = "/standard/drgRule/queryDetailList/" + param.iDrgsId + "/Fare";
    var groupFare = getGroupObj("费用类型");
    tableGroup2.append(groupFare);
    loadServerData(url, null, function (data) {
        for (var i = 0; i < data.datas.length;) {
            var tr = $("<tr></tr>");
            groupFare.find("table").append(tr);
            for (var j = 0; j < 4; j++) {
                if (i < data.datas.length) {
                    tr.append($("<td>" + data.datas[i].classname + "【" + (data.datas[i].maxvalue || 0) + "元】</td>"));
                    i++;
                } else {
                    break;
                }
            }
        }
    })

    var tableGroup3 = $("<div class='tableGroups'></div>");
    $(".detailWindow .detailBody").append(tableGroup3);
    //除外项目
    var url = "/standard/drgRule/queryDetailList/" + param.iDrgsId + "/Except";
    var groupExcept = getGroupObj("除外项目");
    tableGroup3.append(groupExcept);
    loadServerData(url, null, function (data) {
        for (var i = 0; i < data.datas.length;) {
            var tr = $("<tr></tr>");
            groupExcept.find("table").append(tr);
            for (var j = 0; j < 3; j++) {
                if (i < data.datas.length) {
                    tr.append($("<td>" + data.datas[i].fareitemname + "【" + (data.datas[i].nnum || 1) + "个以上】</td>"));
                    i++;
                } else {
                    break;
                }
            }
        }

    })
}

function loadDetail_rjss(param) {
    showDetail();
    $(".detailWindow .title").html(param.title);
    $(".detailWindow .subTitle").html(param.Mictypename + "<span>" + param.ybType + "</span>");
    $(".detailWindow .ruleInfo").html("");
    $(".detailWindow .ruleInfo").append('<div class="ruleTitle">主要治疗方式：</div>');
    $(".detailWindow .ruleInfo").append('<div class="ruleContent">' + (param.opname || "") + '</div>');
    $(".detailWindow .detailFoot .info").html("<h3>启用日期：" + (param.dstartdate || "") + "</h3>");
    $(".detailWindow .tableGroups").remove("");

    var tableGroup1 = $("<div class='tableGroups'></div>");
    $(".detailWindow .detailBody").append(tableGroup1);
    //疾病
    var url = "/standard/dayoperationRule/queryDetailList/" + param.Dayoperationid + "/Drg";
    var groupDrg = getGroupObj("适用疾病");
    tableGroup1.append(groupDrg);
    loadServerData(url, null, function (data) {
        for (var i = 0; i < data.datas.length;) {
            var tr = $("<tr></tr>");
            groupDrg.find("table").append(tr);
            for (var j = 0; j < 3; j++) {
                if (i < data.datas.length) {
                    tr.append($("<td>" + data.datas[i].Icdname + "【" + data.datas[i].Icdcode + "】</td>"));
                    i++;
                } else {
                    break;
                }
            }
        }


    })

    var tableGroup2 = $("<div class='tableGroups'></div>");
    $(".detailWindow .detailBody").append(tableGroup2);
    //术前费用
    var url = "/standard/dayoperationRule/queryDetailList/" + param.Dayoperationid + "/Before";
    var groupFare = getGroupObj("术前费用");
    tableGroup2.append(groupFare);
    loadServerData(url, null, function (data) {
        for (var i = 0; i < data.datas.length;) {
            var tr = $("<tr></tr>");
            groupFare.find("table").append(tr);
            for (var j = 0; j < 3; j++) {
                if (i < data.datas.length) {
                    tr.append($("<td>" + data.datas[i].fareitemname + "【" + data.datas[i].stdprice + "元 X " + (data.datas[i].nnum || 1) + "】</td>"));
                    i++;
                } else {
                    break;
                }
            }
        }

    })

    var tableGroup3 = $("<div class='tableGroups'></div>");
    $(".detailWindow .detailBody").append(tableGroup3);
    //术后费用
    var url = "/standard/dayoperationRule/queryDetailList/" + param.Dayoperationid + "/Current";
    var groupExcept = getGroupObj("术后费用");
    tableGroup3.append(groupExcept);
    loadServerData(url, null, function (data) {
        for (var i = 0; i < data.datas.length;) {
            var tr = $("<tr></tr>");
            groupExcept.find("table").append(tr);
            for (var j = 0; j < 3; j++) {
                if (i < data.datas.length) {
                    tr.append($("<td>" + data.datas[i].fareitemname + "【X" + data.datas[i].nnum + "】</td>"));
                    i++;
                } else {
                    break;
                }
            }
        }
    })
}

function loadDetail_yy(param) {
    showDetail();
    if (param.MictypeId == 1) {
        param.ybType = "省医保"
    } else if (param.MictypeId == 2) {
        param.ybType = "市医保"
    } else {
        param.ybType = param.MictypeId
    }
    $(".detailWindow .title").html(param.title);
    $(".detailWindow .subTitle").html(param.cdgname + "<span>" + param.ybType + "</span>");
    $(".detailWindow .ruleInfo").html("");
    // $(".detailWindow .ruleInfo").append('<div class="ruleTitle">主要治疗方式：</div>');
    // $(".detailWindow .ruleInfo").append('<div class="ruleContent">'+param.opname+'</div>');
    $(".detailWindow .detailFoot .info").html("<h3>启用日期：" + param.dstartdate + "</h3>");
    $(".detailWindow .tableGroups").remove("");


    var tableGroup2 = $("<div class='tableGroups'></div>");
    $(".detailWindow .detailBody").append(tableGroup2);
    //限制规则
    var url = "/standard/drugRule/queryDetailList/" + param.DrugRuleId + "/Rule/ is not null";
    var groupFare = getGroupObj("限制规则");
    tableGroup2.append(groupFare);
    loadServerData(url, null, function (data) {

        for (var i = 0; i < data.datas.length;) {
            var tr = $("<tr></tr>");
            groupFare.find("table").append(tr);
            for (var j = 0; j < 2; j++) {
                if (i < data.datas.length) {
                    tr.append($("<td>" + data.datas[i].Rulenme + "</td>" +
                        "<td>" + data.datas[i].RuleText + "</td>" +
                        "<td>" + (data.datas[i].Minvalue || "") + "~" + (data.datas[i].Maxvalue || "") + "</td>"));
                    i++;
                } else {
                    break;
                }
            }
        }
    })
    var tableGroup1 = $("<div class='tableGroups'></div>");
    $(".detailWindow .detailBody").append(tableGroup1);
    //药品
    var url = "/standard/drugRule/queryDetailList/" + param.DrugRuleId + "/Drug/ is not null";
    var groupDrug = getGroupObj("适用药品");
    tableGroup1.append(groupDrug);
    loadServerData(url, null, function (data) {
        $(data.datas).each(function (index, item) {
            groupDrug.find("table").append($("<tr><td>" + item.cdrugname + "</td>" +
                "<td>" + item.cdrugcode + "</td>" +
                "<td>" + item.jx + "</td>" +
                "<td>" + item.cDgSpecification + "</td>" +
                "<td>" + item.cManufacturer + "</td></tr>"));
        });
    })

    var tableGroup3 = $("<div class='tableGroups'></div>");
    $(".detailWindow .detailBody").append(tableGroup3);
    //限制疾病
    var url = "/standard/drugRule/queryDetailList/" + param.DrugRuleId + "/Drg/ is not null";
    var groupDrg = getGroupObj("限制疾病");
    tableGroup3.append(groupDrg);
    loadServerData(url, null, function (data) {

        for (var i = 0; i < data.datas.length;) {
            var tr = $("<tr></tr>");
            groupDrg.find("table").append(tr);
            for (var j = 0; j < 3; j++) {
                if (i < data.datas.length) {
                    tr.append($("<td>" + data.datas[i].medname + "</td>"));
                    i++;
                } else {
                    break;
                }
            }
        }
    })
}